<?php

namespace App\Admin\Actions\ShopProduct;

use App\Library\Logic\OperateLogLogic;
use App\Models\ShopProductModel;
use Encore\Admin\Actions\Response;
use Encore\Admin\Actions\RowAction;
use Encore\Admin\Facades\Admin;
use Illuminate\Http\Request;

class ShopProductSubPriceAction extends RowAction
{
    public $name = '按件满减';

    public function handle(ShopProductModel $model, Request $request): Response
    {
        $model->sub_price = $request->subPrice;
        $model->save();
        OperateLogLogic::add(Admin::user()->user_id, 'admin.product.subPriceAction');
        return $this->response()->success('操作成功')->refresh();
    }

    public function form(ShopProductModel $model): void
    {
        $this->text('subPrice', __('shop_product.sub_price'))->attribute('onkeyup', 'checkDecimal(this)')
            ->attribute('maxlength', 5)->attribute('onfocus', 'checkDecimal(this)')->default($model->sub_price);
    }
}
